package com.fysports.web.module.menu;

import com.google.common.collect.Lists;
import com.fysports.web.kit.commons.CookiesKit;
import com.fysports.web.kit.tree.ITreeBuilder;
import com.fysports.web.kit.tree.SystemMenuTreeBuilder;
import com.fysports.web.kit.tree.TreeNode;
import com.fysports.web.model.system.SystemMenu;
import com.fysports.web.model.system.SystemRole;
import com.fysports.web.model.system.SystemUser;
import com.fysports.web.module.core.BaseWebManagerModule;
import com.fysports.web.module.core.ModuleException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 包含权限的系统菜单生成
 * Created by konbluesky
 * Date : 15/8/3 上午2:51
 * Project :jdmdata_server
 */
public class RoleMenuModule extends BaseWebManagerModule {

    private final static String REQ_VIEW_LEFTMENU = "leftMenuView";
    private final static String REQ_VIEW_CURNODE = "leftMenuCurNode";
    /*当前节点*/
    //    private String curNode;
    private ITreeBuilder<SystemMenu> builder;


    public RoleMenuModule(HttpServletRequest req) {
        super(req);
    }


    @Override public void execute() throws ModuleException {
        try {
            SystemUser su = CookiesKit.getSystemUser(getRequest());
            if (su == null)
                throw new ModuleException("无效用户,请检查用户");

            SystemRole sr = su.getRole();
            //非管理员,并且无角色菜单的
            if (sr == null && !sr.isAdmin())
                throw new ModuleException("当前用户没有分配角色");


            builder = new SystemMenuTreeBuilder();
            List<TreeNode<SystemMenu>> list = builder.builderTree(sr.getRoleMenu());
            if (list == null || list.isEmpty())
                list = Lists.newArrayList();

            setAttr(REQ_VIEW_LEFTMENU, list);
            //            setAttr(REQ_VIEW_CURNODE, curNode);
        }
        catch (Exception e) {
            throw new ModuleException(e);
        }
    }
}
